<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/5/16
 * Time: 16:09
 */

namespace Biz\tools;

use Doraemon\tools\Curl;

class SensorsApiModel{
    private $host;
    private $token;
    private $project;

    private $event;
    private $aggregator;
    private $field;
    private $from_date;
    private $to_date;
    private $unit;
    private $rollup_date = false;
    private $oFilter;

    const UNIT_DAY = 'day';

    public function __construct($host , $token , $project){
        $this->host = $host;
        $this->token = $token;
        $this->project = $project;
        return $this;
    }
    public function init_query($event , $aggregator = 'general' , $field = ''){
        $this->event = $event;
        $this->aggregator = $aggregator;
        $this->field = $field;
        $this->from_date = date('Y-m-d');
        $this->to_date = date('Y-m-d');
        $this->unit = self::UNIT_DAY;
        $this->oFilter = null;
        return $this;
    }
    public function setDateRange($from_date , $to_date = ''){
        $this->from_date = $from_date;
        $this->to_date = $to_date ? $to_date : date('Y-m-d');
        return $this;
    }
    public function setUnit($unit=self::UNIT_DAY){
        $this->unit = $unit;
        return $this;
    }
    public function setRollupDate($v=true)
    {
        $this->rollup_date = $v;
        return $this;
    }
    public function setFilter($oSensorsApiFilter){
        $this->oFilter = $oSensorsApiFilter;
        return $this;
    }

    public function query(){
        return $this->curl($this->_format_query());
    }
    private function _format_query(){
        $data = array(
            'measures' => array(
                array(
                    'event_name' => $this->event,
                    'aggregator' => $this->aggregator
                )
            ),
            'unit' => $this->unit,
            'to_date' => $this->to_date,
            'from_date' => $this->from_date,
        );
        if($this->field){
            $data['measures']['field']=$this->field;
        }
        if($this->rollup_date){
            $data['rollup_date'] = true;
        }
        if(is_object($this->oFilter)){
            $data['filter'] = $this->oFilter->getFormated();
        }
        return json_encode($data);
    }
    private function curl($json){

        $url = "http://".$this->host."/api/events/report?token=".$this->token."&project=".$this->project;

        $cmd = "curl 'http://".$this->host."/api/events/report?token=".$this->token."&project=".$this->project
            ."' -H 'Content-Type: application/json' --data-binary '".$json."'";

        $rs = $this->run_cmd($cmd);

        return $rs;
    }

    public function get_total_bydate(){
        $this->setRollupDate(true);
        $rs = $this->query();
        $rs = $this->decode_result($rs);
        return $rs['rows'] ?  $rs['rows'][0]['values'][0][0] : 0;
    }
    private function decode_result($rs){
        return json_decode($rs , 1);
    }

    function run_cmd($cmd) {

        $fp = popen( $cmd, 'r' );
        $rs = '';
        while ( ! feof( $fp ) )
            $rs .= fgets( $fp );
        fclose( $fp );

        return $rs;
    }
}